package com.jzqf.home.ui.components

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import kotlinx.coroutines.delay


// 假设我们有一个图片URL列表
val bannerImages = listOf(
    "https://oss.jianyejia.cn/pics/2023/05/26/095526.7257d288d21a426dbeee01daa1263d9f.png",
    "https://oss.jianyejia.cn/pics/2023/05/12/d89198f3d08045498d57e1466e6feaed.jpg",
    // 更多图片链接...
)

// 自定义PageIndicator
@Composable
fun PageIndicator(currentPage: Int, pageCount: Int, modifier: Modifier = Modifier) {
    Row(
        horizontalArrangement = Arrangement.Center, modifier = modifier.fillMaxWidth()
    ) {
        (0 until pageCount).forEach { index ->
            val alpha by animateFloatAsState(if (index == currentPage) 1f else 0.3f, label = "")
            Box(
                Modifier
                    .size(8.dp)
                    .background(Color.Gray.copy(alpha = alpha)),
                contentAlignment = Alignment.Center
            ) {
                if (index == currentPage) {
                    CircularProgressIndicator(
                        color = Color.White, strokeWidth = 2.dp, modifier = Modifier.size(6.dp)
                    )
                }
            }
        }
    }
}

// 自动轮播的Banner组件
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun AutoCarouselBanner(banners: List<String>) {
    var currentPage by remember { mutableStateOf(0) }
    val pagerState = rememberPagerState(pageCount = { banners.size })

    LaunchedEffect(key1 = Unit) {
        while (true) {
            delay(3000L) // 每隔3秒切换一次
            with(pagerState) {
                val nextIndex = (currentPage + 1) % banners.size
//                scrollToItem(nextIndex)
                currentPage = nextIndex
            }
        }
    }

    Column(Modifier.fillMaxWidth()) {
        HorizontalPager(
            state = pagerState,
        ) { pageIndex ->
            AsyncImage(
                model = banners[pageIndex],
                contentDescription = "Banner Image",
                modifier = Modifier
                    .fillMaxWidth()
                    .height(294.dp),
                contentScale = ContentScale.Crop,
            )
        }

        // 显示页码指示器
        PageIndicator(currentPage = currentPage, pageCount = banners.size)
    }
}

@Preview
@Composable
fun BannerPreview() {
    AutoCarouselBanner(bannerImages)
}

@Composable
fun BannerComponent() {
    AutoCarouselBanner(bannerImages)
}
